<?php
class Show3DController extends BaseManageController{
	public function actionAdmin(){
		$this->pageTitle = '360展示管理列表 - ' . Yii::app()->name;
		$mod = new Show360Model();
		$con = array(
			'site_id' => SITE_ID
		);
		$total = $mod->count($con);
		$this->setPagination($total);
		$show_360_list = $mod->find($con, null, $this->pager->offset, $this->pager->pageSize);
		$this->render('admin', array('list'=>$show_360_list));
	}
	
	public function actionEdit($id = null, $act=null){
		if(Yii::app()->request->isPostRequest){
			$this->_save($id);
		}
		if(!empty($act)){
			$allowed_acts = array('_split_imgs');
			if(in_array($act, $allowed_acts)){
				$this->$act($id);
			}
			exit;
		}
		$this->pageTitle = '3D展示编辑 - ' . Yii::app()->name;
		if($id == null){
			$show3d = array(
				'_id' => '',
			);
		}else{
			$mod = new Show360Model();
			$show3d = $mod->get_by_key($id);
			if($show3d['site_id'] != SITE_ID){
				throw new CHttpException(403, '非法请求！');
			}
		}
		$this->render('edit', array('model'=>$show3d));
	}
	
	private function _save($id){
		if(!isset($_POST['Show360'])){
			$this->ajax_error('非法请求！');
		}
		$show360 = $_POST['Show360'];
		$mod = new Show360Model();
		if($id === null){
			$id = CommonHelper::gen_uuid();
			$base_path = '360/' . date('Y/m/') . $id;
		}else{
			$show_360_mod = $mod->get_by_key($id, 'base_path');
			$base_path = $show_360_mod['base_path'];
		}

		//处理图片
		$this->init_pictures($base_path, $show360, array('save_path'=>false, 'has_main_image'=>false));
		if(isset($show360['pictures']) && !empty($show360['pictures'])){
			$picture_item = $show360['pictures'][0];
			if(!isset($show360['width'])){
				//如果没有设置高宽
				$show360['width'] = $picture_item['width'];
				$show360['height'] = $picture_item['height'];
			}
			if(!isset($picture_item['type'])){
				$show360['type'] = preg_replace('/^.*\.(.*)$/', '$1', $picture_item['path']);
			}
		}
		
		if(empty($show360['_id'])){
			//新建
			$show360 = array_merge($show360, array(
				'_id' 				=> $id,
				'create_user_id' 	=> intval(Yii::app()->user->id),
				'create_time' 		=> time(),
				'site_id' 			=> SITE_ID,
				'base_path' 		=> $base_path,
				'servial' 			=> implode(',', Show360Model::gen_serial()),
			));
			$mod->insert($show360);
		}else{
			$mod->update_by_key($id, $show360);
		}
		$this->ajax_info('保存成功！', $id);
	}
	
	public function actionSet(){
		
	}
	
	public function actionIndex($id){
		$this->pageTitle = '3D展示预览 - ' . Yii::app()->name;
		$this->layout = $this->module->getLayout('empty');
		$this->render('index', array(
			'id' => $id,
		));
	}

	/**
	 * 切割图片
	 *
	 * @param 商品ID $id
	 * @param 图片路径 $image_paths
	 */
	private function _split_imgs($id){
		set_time_limit(0);
		$mod = new Show360Model();
		$show_360 = $mod->get_by_key($id);
		$image_paths = $show_360['pictures'];
		$base_360_base_path = ROOT_PATH . IMAGE_PATH . $show_360['base_path'];
		CommonHelper::ymkdirs($base_360_base_path .  '/3d/');
		
		$serials = explode(',', $show_360['servial']);
	
		$type = $show_360['type'];
		$index = intval($_REQUEST['index']);
		if(isset($image_paths[$index])){
			$image_item = $image_paths[$index];
			$img_path = $base_360_base_path . '/' . $image_item['path'];
			$this->_create_360_image($base_360_base_path .  '/3d/', $img_path, $serials, $index + 1, $type);
			$this->ajax_info('true');
		}else{
			$this->ajax_error('error=' + index);
		}
	}
	
	/**
	 * @param base_360_path
	 * @param $img_path
	 * @param serials
	 * @param clock
	 */
	private function _create_360_image($base_360_path, $img_path, $serials, $clock, $type) {
		if(!is_file($img_path)){
			// 图片不存在
			return array('result'=>false, 'info'=>'图片不存在！');
		}
		$zoom_count = 1;
		list($width, $height) = getimagesize($img_path);
		$max_size = max(array($width, $height));
		while($max_size > Show360Model::MIN_ZOOM_SIZE){
			// 计算可放大倍数
			++$zoom_count;
			$max_size /= 2;
		}
		$handle = new ImageHandler();
			
		$file_name = preg_replace('%.*?([^.|/]*\.jpg|jpeg|gif|png)%i', '$1', $img_path);
			
		// 生成压缩图
		for($z = $zoom_count; $z >= 1; --$z){
			$zoom = $zoom_count - $z + 1;
			$rate = pow(2, $z - 1);
			$zoom_img_w = intval($width / $rate);
			$zoom_img_h = intval($height / $rate);
				
			// 缩略图临时路径
			$des_path = $base_360_path . $serials[$zoom] . '_' . $serials[$clock] . '.' . $type;
// 			$des_path = $base_360_path . $zoom . '_' . $clock . '.' . $type;
			if(!is_file($des_path)){
				$handle->setSrcImg($img_path);
				$handle->setDstImg($des_path);
				$handle->createImg($zoom_img_w, $zoom_img_h);
			}
			if($zoom < 2){
				// 不需要剪切了
			}else{
				// 进一步剪切
				$handle->split_360($des_path, $base_360_path, Show360Model::SPLIT_SIZE, Show360Model::SPLIT_SIZE, $clock, $zoom, $serials);
// 				$handle->split_360($des_path, $base_360_path, Show360Model::SPLIT_SIZE, Show360Model::SPLIT_SIZE, $clock, $zoom);
				// 删除大图
				if($zoom > 2){
					unlink($des_path);
				}
			}
		}
		return array('result'=>true, 'info'=>'', 'zoom'=>$zoom_count);
	}
}

?>